<script>
import Popconfirm from '@/components/Popconfirm/popconfirm.vue'
import { delDorm } from '@/api/v2/dorm'
export default {
  components: {
    Popconfirm
  },
  props: {
    apartmentId: {
      type: Number
    }
  },
  data() {
    return {

    }
  },
  methods: {
    sleep(t) {
      return new Promise(r => setTimeout(r, t))
    },
    async deleteApartment() {
      if (typeof this.apartmentId !== 'number') return
      await this.sleep(300)
      return await delDorm({ id: `[${this.apartmentId}]` })
    },
    handleDeleteFailed(e) {
      this.$message.error(e?.response?.data?.message ?? '删除失败！')
    },
    handleDeleteSuccess(e) {
      const { code, message } = e
      if (code === 201) {
        this.$message.success(message ?? '删除成功')
        this.$emit('success', e)
      } else {
        this.$message.error(message ?? e.response.data.message ?? '删除失败')
      }
    }
  }
}
</script>

<template>
  <Popconfirm
    :confirm-fn="deleteApartment"
    :is-text-btn="true"
    @success="handleDeleteSuccess"
    @fail="handleDeleteFailed"
  />
</template>
